package com.incar.chuxing.utils.ble.bean;

import com.incar.chuxing.utils.ble.utils.ConvertHelper;

/**
 * Created by xiongjida on 2017/7/7.
 */

public class ZisMileRangeBean extends ZisBaseBean {


    public int CmdID;
    public String RemainBattery;//剩余电量
    public String MileRange;//续航里程
    public String TotalMileage;
    public String Rpm;//转速
    public String Speed;


    private ZisMileRangeBean() {
    }

    /**
     * @param idc
     * @param receivedTime
     * @param bytes
     * @param start
     * @return 7e10 001c 01cd 0299 00c40100010000010000000000ffff00ffff595f3cfafe09
     * 00a1 0299 0000015200ffff010000118900ffff00ffff595f5c30
     */
    public static ZisMileRangeBean CreateBean(String idc, long receivedTime, byte[] bytes, int start) {
        ZisMileRangeBean bean = new ZisMileRangeBean();
        bean.IDC = idc;
        bean.ReceivedTime = receivedTime;

        int n = start + 2;

        if (bytes[n] == 0x01)
            bean.RemainBattery = String.valueOf(bytes[n + 1]);
        else if (bytes[n] == 0x02)
            bean.RemainBattery = String.valueOf(-1);
        else if (bytes[n] == 0x03)
            bean.RemainBattery = String.valueOf(-2);
        else
            bean.RemainBattery = null;
        n += 2;

        if (bytes[n] == 0x01)
            bean.MileRange = String.valueOf(ConvertHelper.bytesToInt(bytes[n + 1], bytes[n + 2]));
        else if (bytes[n] == 0x02)
            bean.MileRange = String.valueOf(-1);
        else if (bytes[n] == 0x03)
            bean.MileRange = String.valueOf(-2);
        else
            bean.MileRange = null;
        n += 3;

        if (bytes[n] == 0x01)
            bean.TotalMileage = String.valueOf(ConvertHelper.bytesToInt(bytes[n + 1], bytes[n + 2], bytes[n + 3], bytes[n + 4]));
        else if (bytes[n] == 0x02)
            bean.TotalMileage = String.valueOf(-1);
        else if (bytes[n] == 0x03)
            bean.TotalMileage = String.valueOf(-2);
        else
            bean.TotalMileage = null;
        n += 5;


        if (bytes[n] == 0x01)
            bean.Rpm = String.valueOf(ConvertHelper.bytesToInt(bytes[n + 1], bytes[n + 2]));
        else if (bytes[n] == 0x02)
            bean.Rpm = String.valueOf(-1);
        else if (bytes[n] == 0x03)
            bean.Rpm = String.valueOf(-2);
        else
            bean.Rpm = null;
        n += 3;

        if (bytes[n] == 0x01)
            bean.Speed = String.valueOf(ConvertHelper.bytesToInt(bytes[n + 1], bytes[n + 2]));
        else if (bytes[n] == 0x02)
            bean.Speed = String.valueOf(-1);
        else if (bytes[n] == 0x03)
            bean.Speed = String.valueOf(-2);
        else
            bean.Speed = null;
        n += 3;


        bean.CmdID = ConvertHelper.bytesToInt(bytes[n], bytes[n + 1], bytes[n + 2], bytes[n + 3]);
        n += 4;

        return bean;
    }

//    public static ZisMileRangeBean CreateBean(IPrimitiveMap map) {
//        try {
//            string idc = map.GetString("IDC");
//            long receivedTime = map.GetLong("RECEIVETIME");
//            byte[] bytes = map.GetBytes("PROTOCOLDATA");
//
//            return CreateBean(idc, receivedTime, bytes, 0);
//        } catch (Exception ex) {
//            throw new Exception("ZisMileRangeBean data ERROR." + ex.Message);
//        }
//    }

//    public override String
//
//    ToString() {
//        return string.Format("IDC={0},RemainBattery={1},MileRange={2},TotalMileage={3}",
//                IDC, RemainBattery, MileRange, TotalMileage);
//    }


    @Override
    public String toString() {
        return "ZisMileRangeBean{" +
                "\nCmdID=" + CmdID +
                ", \nRemainBattery(剩余电量)='" + RemainBattery + '\'' +
                ", \nMileRange(续航里程)='" + MileRange + '\'' +
                ", \nTotalMileage(总里程)='" + TotalMileage + '\'' +
                ", \nRpm(转速)='" + Rpm + '\'' +
                ", \nSpeed(速度)='" + Speed + '\'' +
                "\n}";
    }
}
